package com.mystnihon.freeremote.settings;

import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.config.XmlTag;
import com.mystnihon.freeremote.db.DBHelper;
import com.mystnihon.freeremote.db.table.SeedboxTable;

public class SettingsFactory {

	public static String createXML(Context context) {
		Document doc;
		try {
			doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

			// Create root element
			Element root = doc.createElement(XmlTag.TAG_ROOT);
			doc.appendChild(root);

			// Get the seedbox list
			root.appendChild(createSeedboxsListElement(context, doc));

			// Get the settings
			root.appendChild(createSettingListElement(context, doc));

			// Create transformer to write file
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
			transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");

			// Write xml to string
			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			transformer.transform(new DOMSource(doc), result);

			// return XML string
			return writer.toString();

		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		return "";
	}

	private static Element createSeedboxsListElement(Context context, Document doc) {
		Element seedboxs = doc.createElement(XmlTag.TAG_SEEDBOXS);
		ArrayList<SeedboxTable> seedboxList = DBHelper.getListOfSeedbox(context.getContentResolver());
		for (SeedboxTable seedboxTable : seedboxList) {
			// Create seedbox tag
			Element seedbox = doc.createElement(XmlTag.TAG_SEEDBOX);

			// Create infos tags
			Element name = doc.createElement(XmlTag.TAG_NAME);
			name.setTextContent(seedboxTable.mName);
			Element host = doc.createElement(XmlTag.TAG_HOST);
			host.setTextContent(seedboxTable.mHost);
			Element login = doc.createElement(XmlTag.TAG_LOGIN);
			login.setTextContent(seedboxTable.seedboxLogin);
			Element password = doc.createElement(XmlTag.TAG_PASSWORD);
			CDATASection cdata = doc.createCDATASection(seedboxTable.seedboxPassword);
			password.appendChild(cdata);
			Element port = doc.createElement(XmlTag.TAG_PORT);
			port.setTextContent(String.valueOf(seedboxTable.seedboxPort));

			seedbox.appendChild(name);
			seedbox.appendChild(host);
			seedbox.appendChild(login);
			seedbox.appendChild(password);
			seedbox.appendChild(port);

			seedboxs.appendChild(seedbox);

		}
		return seedboxs;
	}

	private static Element createSettingListElement(Context context, Document doc) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String remotecode = prefs.getString(context.getString(R.string.pref_key_remote_code), "");
		boolean autoStart = prefs.getBoolean(context.getString(R.string.pref_key_auto_start), false);
		int refreshTime = prefs.getInt(context.getString(R.string.pref_key_seedbox_refresh_time), 3);

		Element settings = doc.createElement(XmlTag.TAG_SETTINGS);

		// create remote code
		Element remote = doc.createElement(XmlTag.TAG_REMOTE);
		remote.setAttribute(XmlTag.ATTR_CODE, remotecode);
		Element seedbox = doc.createElement(XmlTag.TAG_SEEDBOX);
		seedbox.setAttribute(XmlTag.ATTR_AUTOSTART, String.valueOf(autoStart));
		seedbox.setAttribute(XmlTag.ATTR_REFRESH_TIME, String.valueOf(refreshTime));

		settings.appendChild(remote);
		settings.appendChild(seedbox);

		return settings;
	}
}
